with System;
with Ada.Unchecked_Conversion;

package body FT2.Glyphs is

   --------------
   -- To_Glyph --
   --------------

   function To_Glyph (X : FT_BitmapGlyph) return FT_Glyph is
      function To_Target is new Ada.Unchecked_Conversion
        (FT_BitmapGlyph, FT_Glyph);
   begin
      return To_Target (X);
   end To_Glyph;

   --------------------
   -- To_BitmapGlyph --
   --------------------

   function To_BitmapGlyph (X : FT_Glyph) return FT_BitmapGlyph is
      function To_Target is new Ada.Unchecked_Conversion
        (FT_Glyph, FT_BitmapGlyph);
   begin
      if X /= null and then X.Format = FT_GLYPH_FORMAT_BITMAP then
         return To_Target (X);
      else
         return null;
      end if;
   end To_BitmapGlyph;

   --------------
   -- To_Glyph --
   --------------

   function To_Glyph (X : FT_OutlineGlyph) return FT_Glyph is
      function To_Target is new Ada.Unchecked_Conversion
        (FT_OutlineGlyph, FT_Glyph);
   begin
      return To_Target (X);
   end To_Glyph;

   ---------------------
   -- To_OutlineGlyph --
   ---------------------

   function To_OutlineGlyph (X : FT_Glyph) return FT_OutlineGlyph is
      function To_Target is new Ada.Unchecked_Conversion
        (FT_Glyph, FT_OutlineGlyph);
   begin
      if X /= null and then X.Format = FT_GLYPH_FORMAT_OUTLINE then
         return To_Target (X);
      else
         return null;
      end if;
   end To_OutlineGlyph;

   ---------------
   -- Get_Glyph --
   ---------------

   procedure Get_Glyph
     (Slot : FT_GlyphSlot;
      Glyph : out FT_Glyph;
      Error : out FT_Error)
   is
      function Internal
        (Slot : FT_GlyphSlot;
         Glyph : System.Address)
         return FT_Error;
      pragma Import (C, Internal, "FT_Get_Glyph");
   begin
      Error := Internal (Slot, Glyph'Address);
   end Get_Glyph;

   ----------
   -- Copy --
   ----------

   procedure Copy
     (Source : FT_Glyph;
      Target : out FT_Glyph;
      Error : out FT_Error)
   is
      function Internal
        (Source : FT_Glyph;
         Target : System.Address)
         return FT_Error;
      pragma Import (C, Internal, "FT_Glyph_Copy");
   begin
      Error := Internal (Source, Target'Address);
   end Copy;

   ---------------
   -- Transform --
   ---------------

   procedure Transform
     (Glyph : FT_Glyph;
      Matrix : FT_Matrix;
      Vector : FT_Vector;
      Error : out FT_Error)
   is
      function Internal
        (Glyph : FT_Glyph;
         Matrix : FT_Matrix;
         Vector : FT_Vector)
         return FT_Error;
      pragma Import (C, Internal, "FT_Glyph_Transform");
   begin
      Error := Internal (Glyph, Matrix, Vector);
   end Transform;

   ---------------------
   -- Glyph_To_Bitmap --
   ---------------------

   procedure Glyph_To_Bitmap
     (Glyph : in out FT_Glyph;
      Render_Mode : FT_Render_Mode;
      Origin : FT_Vector;
      Destroy : Boolean;
      Error : out FT_Error)
   is
      function Internal
        (Glyph : System.Address;
         Render_Mode : FT_Render_Mode;
         Origin : FT_Vector;
         Destroy : FT_Bool)
         return FT_Error;
      pragma Import (C, Internal, "FT_Glyph_To_Bitmap");
   begin
      Error := Internal (Glyph'Address,
                         Render_Mode,
                         Origin,
                         Boolean'Pos (Destroy));
   end Glyph_To_Bitmap;

end FT2.Glyphs;
